package org.ezplatform.office.questionnaire.web.rest;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.ezplatform.cloud.api.component.attachment.AttachmentUtilsService;
import org.ezplatform.core.common.WebUser;
import org.ezplatform.core.exception.ValidationException;
import org.ezplatform.core.pagination.PropertyFilter;
import org.ezplatform.core.service.BaseService;
import org.ezplatform.core.web.controller.BaseController;
import org.ezplatform.core.web.util.WebUtils;
import org.ezplatform.core.web.view.OperationResult;
import org.ezplatform.office.questionnaire.entity.Questionnaire;
import org.ezplatform.office.questionnaire.entity.QuestionnaireAnswer;
import org.ezplatform.office.questionnaire.entity.QuestionnaireSet;
import org.ezplatform.office.questionnaire.entity.QuestionnaireSetItems;
import org.ezplatform.office.questionnaire.entity.QuestionnaireSetTopic;
import org.ezplatform.office.questionnaire.entity.QuestionnaireTopic;
import org.ezplatform.office.questionnaire.entity.QuestionnaireTopicOptions;
import org.ezplatform.office.questionnaire.service.QuestionnaireService;
import org.ezplatform.office.questionnaire.service.QuestionnaireSetItemsService;
import org.ezplatform.office.questionnaire.service.QuestionnaireSetService;
import org.ezplatform.office.questionnaire.service.QuestionnaireSetTopicService;
import org.ezplatform.office.questionnaire.service.QuestionnaireTopicOptionsService;
import org.ezplatform.office.questionnaire.service.QuestionnaireTopicService;
import org.ezplatform.util.GlobalConstant;
import org.ezplatform.util.JsonUtils;
import org.ezplatform.util.StringUtils;
import org.ezplatform.util.UuidUtils;
import org.ezplatform.util.ValidateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

@RestController
@RequestMapping(value = "/api/w/questionnaire/set/topic")
public class QuestionnaireSetTopicWebApiController extends BaseController<QuestionnaireSetTopic, String> {

	@Autowired
	private QuestionnaireSetTopicService topicService;

	@Autowired
	private QuestionnaireSetItemsService itemService;

	@Autowired
	private AttachmentUtilsService attachmentUtilsService;

	@Autowired
	private QuestionnaireSetService questionnaireSetService;
	
	@Autowired
	private QuestionnaireTopicService queTopicService;
	
	@Autowired
	private QuestionnaireTopicOptionsService optionService;
	
	@Autowired
	private QuestionnaireService questionnaireService;

	@Override
	protected BaseService<QuestionnaireSetTopic, String> getEntityService() {
		return topicService;
	}

	/**
	 * 查询题库题目
	 * 
	 * @param entity
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/getDesign", method = RequestMethod.GET)
	@ResponseBody
	public OperationResult getDesign(HttpServletRequest request) {
		WebUser user = WebUtils.getCurrentUser();
		String setId = request.getParameter("setId");
		if (setId != null && !ValidateUtils.validateId(setId)) {
			throw new ValidationException("非法参数");
		}
		String topicType = request.getParameter("topicType");
		if (topicType != null && !ValidateUtils.validateParam(topicType)) {
			throw new ValidationException("非法参数");
		}
		String topicName = request.getParameter("topicName");
		if (topicType != null && !ValidateUtils.validateParam(topicName)) {
			throw new ValidationException("非法参数");
		}
		String topicIds = request.getParameter("topicIds");
		Map<String, Object> map = new HashMap<>();

		String userId = WebUtils.getCurrentUser().getUserId();
		List<QuestionnaireSetTopic> list = topicService.getSetTopicList(setId, topicType, topicName, topicIds, userId, user.getJwtToken());
		map.put("topicSetting", list);
		/*
		 * 获取附件id
		 */
		if(list != null && list.size() > 0) {
			for (QuestionnaireSetTopic questionnaireSetTopic : list) {
				if(questionnaireSetTopic.getList() != null && questionnaireSetTopic.getList().size() > 0) {
					for(int j=0;j<questionnaireSetTopic.getList().size();j++){
						QuestionnaireSetItems option = questionnaireSetTopic.getList().get(j);
						String jsonVal = option.getItemSetting();
						QuestionnaireAnswer answer = new QuestionnaireAnswer();
						if(StringUtils.isNotEmpty(jsonVal) && !jsonVal.contains("option-option")) {
							JSONObject jsStr = JSONObject.parseObject(jsonVal);
							if( !"".equals(jsStr.getString("optionImg")) && jsStr.getString("optionImg") != null) {
								String fileInfo =attachmentUtilsService.getAttachmentInfo(jsStr.getString("optionImg"),user.getJwtToken());
							    answer.setAttachment("["+fileInfo+"]");
							    List<Map<String, String>> fileInfoList =attachmentUtilsService.getAttachmentByIds(jsStr.getString("optionImg"), WebUtils.getCurrentUser().getCorpId(), user.getJwtToken());
								String url = "/attachment/image?fid="+jsStr.getString("optionImg")+"&chksum="+fileInfoList.get(0).get("checksum");
							    answer.setUrlPic(url);
							}
							option.setAnswer(answer);
						}
					}
				}
			}
		}
		QuestionnaireSet questionnaireSet = questionnaireSetService.findOne(setId);
		map.put("entity", questionnaireSet);
		map.put("flexServerPath", GlobalConstant.getFlexServerPath(request));
		return OperationResult.buildSuccessResult(map);
	}
	
	/**
	 * 分页查询题库题目
	 * 
	 * @param entity
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/getDesignByPage", method = RequestMethod.GET)
	@ResponseBody
	public OperationResult getDesignByPage(HttpServletRequest request) {
		WebUser user = WebUtils.getCurrentUser();
		String setId = request.getParameter("setId");
		if (setId != null && !ValidateUtils.validateId(setId)) {
			throw new ValidationException("非法参数");
		}
		String topicType = request.getParameter("topicType");
		if (topicType != null && !ValidateUtils.validateParam(topicType)) {
			throw new ValidationException("非法参数");
		}
		String topicName = request.getParameter("topicName");
		if (topicType != null && !ValidateUtils.validateParam(topicName)) {
			throw new ValidationException("非法参数");
		}
		String currentPage = request.getParameter("page");
		if (currentPage != null && !ValidateUtils.validateParam(currentPage)) {
			throw new ValidationException("非法参数");
		}
		Pageable pageable = PropertyFilter.buildPageableFromHttpRequest(request);
		Map<String, Object> map = new HashMap<>();

		String userId = WebUtils.getCurrentUser().getUserId();
		Map<String, Object> topicSettingMap = topicService.getSetTopicListByPage(setId, topicType, topicName, userId, user.getJwtToken(), pageable, currentPage);
		map.put("topicSetting", topicSettingMap);
		List<QuestionnaireSetTopic> list = (List<QuestionnaireSetTopic>)topicSettingMap.get("content");
		/*
		 * 获取附件id
		 */
		if(list != null && list.size() > 0) {
			for (QuestionnaireSetTopic questionnaireSetTopic : list) {
				if(questionnaireSetTopic.getList() != null && questionnaireSetTopic.getList().size() > 0) {
					for(int j=0;j<questionnaireSetTopic.getList().size();j++){
						QuestionnaireSetItems option = questionnaireSetTopic.getList().get(j);
						String jsonVal = option.getItemSetting();
						QuestionnaireAnswer answer = new QuestionnaireAnswer();
						if(StringUtils.isNotEmpty(jsonVal) && !jsonVal.contains("option-option")) {
							JSONObject jsStr = JSONObject.parseObject(jsonVal);
							if( !"".equals(jsStr.getString("optionImg")) && jsStr.getString("optionImg") != null) {
								String fileInfo =attachmentUtilsService.getAttachmentInfo(jsStr.getString("optionImg"),user.getJwtToken());
							    answer.setAttachment("["+fileInfo+"]");
							    List<Map<String, String>> fileInfoList =attachmentUtilsService.getAttachmentByIds(jsStr.getString("optionImg"), WebUtils.getCurrentUser().getCorpId(), user.getJwtToken());
								String url = "/attachment/image?fid="+jsStr.getString("optionImg")+"&chksum="+fileInfoList.get(0).get("checksum");
							    answer.setUrlPic(url);
							}
							option.setAnswer(answer);
						}
					}
				}
			}
		}
		QuestionnaireSet questionnaireSet = questionnaireSetService.findOne(setId);
		map.put("entity", questionnaireSet);
		map.put("flexServerPath", GlobalConstant.getFlexServerPath(request));
		return OperationResult.buildSuccessResult(map);
	}
	
	/**
	 * 保存从题库中选中的题目
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/saveSelectTopicList", method = RequestMethod.POST)
	@ResponseBody
	public OperationResult saveSelectTopicList(HttpServletRequest request) {
		WebUser user = WebUtils.getCurrentUser();
		String questionnaireId = request.getParameter("questionnaireId");
		if (questionnaireId != null && !ValidateUtils.validateId(questionnaireId)) {
			throw new ValidationException("非法参数");
		}
		String setId = request.getParameter("setId");
		if (setId != null && !ValidateUtils.validateId(setId)) {
			throw new ValidationException("非法参数");
		}
		String topicType = request.getParameter("topicType");
		if (topicType != null && !ValidateUtils.validateParam(topicType)) {
			throw new ValidationException("非法参数");
		}
		String topicName = request.getParameter("topicName");
		if (topicType != null && !ValidateUtils.validateParam(topicName)) {
			throw new ValidationException("非法参数");
		}
		String topicIds = request.getParameter("topicIds");
		String selectTopicNum = request.getParameter("selectTopicNum");
		String selectTopicScore = request.getParameter("selectTopicScore");
		String userId = WebUtils.getCurrentUser().getUserId();
		List<QuestionnaireSetTopic> topicList = topicService.getSetTopicList(setId, topicType, topicName, topicIds,
				userId, user.getJwtToken());
		if (CollectionUtils.isNotEmpty(topicList)) {
			List<QuestionnaireTopic> listTopic = new ArrayList<QuestionnaireTopic>();
			List<QuestionnaireTopicOptions> listOptions = new ArrayList<QuestionnaireTopicOptions>();
			long time = new Date().getTime();
			for (int i = 0; i < topicList.size(); i++) {
				QuestionnaireSetTopic questionnaireSetTopic = topicList.get(i);
				QuestionnaireTopic topic = new QuestionnaireTopic();
				topic.setQuestionnaireId(questionnaireId);
				topic.setTopicName(questionnaireSetTopic.getTopicName());
				topic.setTopicNameTxt(questionnaireSetTopic.getTopicNameTxt());
				topic.setTopicNo(questionnaireSetTopic.getTopicNo());
				topic.setTopicSetting(questionnaireSetTopic.getTopicSetting());
				topic.setTopicType(questionnaireSetTopic.getTopicType());
				topic.setOrderNo(0);
				topic.setIsScore(null);
				topic.setTimes(String.valueOf(time + i));
				topic.setId(UuidUtils.UUID());
				listTopic.add(topic);
				for (QuestionnaireSetItems questionnaireSetItems : questionnaireSetTopic.getList()) {
					QuestionnaireTopicOptions option = new QuestionnaireTopicOptions();
					option.setTopicId(topic.getId());
					option.setName(questionnaireSetItems.getItemName());
					option.setOptionOrder(questionnaireSetItems.getSortNo());
					option.setOptionSetting(questionnaireSetItems.getItemSetting());
					listOptions.add(option);
				}
			}
			queTopicService.save(listTopic);
			optionService.save(listOptions);
			Questionnaire questionnaire = this.questionnaireService.findOne(questionnaireId);
			String sumScore = questionnaire.getSumScore();
			if (StringUtils.isNotBlank(sumScore)) {
				questionnaire.setSumScore(String.valueOf(Float.parseFloat(sumScore) + Float.parseFloat(selectTopicScore)));
			} else {
				questionnaire.setSumScore(selectTopicScore);
			}
			String sumTopicNum = questionnaire.getSumTopicNum();
			if (StringUtils.isNotBlank(sumTopicNum)) {
				if(sumScore.indexOf(".")>-1) {
					questionnaire.setSumTopicNum(String.valueOf(Float.parseFloat(sumTopicNum) + Float.parseFloat(selectTopicNum)));
				}else
				questionnaire.setSumTopicNum(String.valueOf(Integer.parseInt(sumTopicNum) + Integer.parseInt(selectTopicNum)));
			} else {
				questionnaire.setSumTopicNum(selectTopicNum);
			}
			this.questionnaireService.save(questionnaire);
		}
		return OperationResult.buildSuccessResult("保存成功");
	}

	/**
	 * 保存题库题目
	 * 
	 * @param entity
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/designEdit", method = RequestMethod.POST)
	@ResponseBody
	public OperationResult designEdit(HttpServletRequest request) {
		List<QuestionnaireSetTopic> listTopic = new ArrayList<QuestionnaireSetTopic>();
		List<QuestionnaireSetItems> listItems = new ArrayList<QuestionnaireSetItems>();
		String isImport = request.getParameter("isImport");
    	if(isImport != null && !ValidateUtils.validateId(isImport)) {
            throw new ValidationException("非法参数");
        }
		String setId = request.getParameter("setId");
		if (setId != null && !ValidateUtils.validateId(setId)) {
			throw new ValidationException("非法参数");
		}
		String[] sinselect = request.getParameterValues("sinselect");
		if (sinselect != null && !ValidateUtils.validateId(sinselect)) {
			throw new ValidationException("非法参数");
		}
		String[] trueOrFalse = request.getParameterValues("trueOrFalse");
		if (trueOrFalse != null && !ValidateUtils.validateId(trueOrFalse)) {
			throw new ValidationException("非法参数");
		}
		String[] multiselect = request.getParameterValues("multiselect");
		if (multiselect != null && !ValidateUtils.validateId(multiselect)) {
			throw new ValidationException("非法参数");
		}
		String[] check = request.getParameterValues("check");
		if (check != null && !ValidateUtils.validateId(check)) {
			throw new ValidationException("非法参数");
		}
		String[] fillInBlanks = request.getParameterValues("fillInBlanks");
		if (fillInBlanks != null && !ValidateUtils.validateId(fillInBlanks)) {
			throw new ValidationException("非法参数");
		}
		String[] answer = request.getParameterValues("answer");
		if (answer != null && !ValidateUtils.validateId(answer)) {
			throw new ValidationException("非法参数");
		}
		String[] shortAnswer = request.getParameterValues("shortAnswer");
		if (shortAnswer != null && !ValidateUtils.validateId(shortAnswer)) {
			throw new ValidationException("非法参数");
		}
		String[] field = request.getParameterValues("field");
		if (field != null && !ValidateUtils.validateId(field)) {
			throw new ValidationException("非法参数");
		}
		String[] scale = request.getParameterValues("scale");
		if (scale != null && !ValidateUtils.validateId(scale)) {
			throw new ValidationException("非法参数");
		}
		String[] mulscale = request.getParameterValues("mulscale");
		if (mulscale != null && !ValidateUtils.validateId(mulscale)) {
			throw new ValidationException("非法参数");
		}
		String[] illustrate = request.getParameterValues("illustrate");
		if (illustrate != null && !ValidateUtils.validateId(illustrate)) {
			throw new ValidationException("非法参数");
		}
		String[] paging = request.getParameterValues("paging");
		if (paging != null && !ValidateUtils.validateId(paging)) {
			throw new ValidationException("非法参数");
		}
		if (sinselect != null && sinselect.length > 0) {
			for (int i = 0; i < sinselect.length; i++) {
				String times = sinselect[i];
				String topicAllSetting = request.getParameter(times + "sinselect");
				String itemSetting = request.getParameter(times + "option");
				Map<String, Object> map = JsonUtils.readValue(topicAllSetting.replaceAll("\r\n|\r|\n", ""));
				QuestionnaireSetTopic topic = new QuestionnaireSetTopic();
				topic = topicService.getSaveTopicInfo(map, setId, "sinselect");
				listTopic.add(topic);
				List<QuestionnaireSetItems> listItem = itemService.getSaveItemsInfo(itemSetting, topic.getId());
				listItems.addAll(listItem);
			}
		}
		if (trueOrFalse != null && trueOrFalse.length > 0) {
			for (int i = 0; i < trueOrFalse.length; i++) {
				String times = trueOrFalse[i];
				String topicAllSetting = request.getParameter(times + "trueOrFalse");
				String itemSetting = request.getParameter(times + "option");
				Map<String, Object> map = JsonUtils.readValue(topicAllSetting.replaceAll("\r\n|\r|\n", ""));
				QuestionnaireSetTopic topic = new QuestionnaireSetTopic();
				topic = topicService.getSaveTopicInfo(map, setId, "trueOrFalse");
				listTopic.add(topic);
				List<QuestionnaireSetItems> listItem = itemService.getSaveItemsInfo(itemSetting, topic.getId());
				listItems.addAll(listItem);
			}
		}
		if (multiselect != null && multiselect.length > 0) {
			for (int i = 0; i < multiselect.length; i++) {
				String times = multiselect[i];
				String topicAllSetting = request.getParameter(times + "multiselect");
				String itemSetting = request.getParameter(times + "option");
				Map<String, Object> map = JsonUtils.readValue(topicAllSetting.replaceAll("\r\n|\r|\n", ""));
				QuestionnaireSetTopic topic = new QuestionnaireSetTopic();
				topic = topicService.getSaveTopicInfo(map, setId, "multiselect");
				listTopic.add(topic);
				List<QuestionnaireSetItems> listItem = itemService.getSaveItemsInfo(itemSetting, topic.getId());
				listItems.addAll(listItem);
			}
		}
		if (check != null && check.length > 0) {
			for (int i = 0; i < check.length; i++) {
				String times = check[i];
				String topicAllSetting = request.getParameter(times + "check");
				String itemSetting = request.getParameter(times + "option");
				Map<String, Object> map = JsonUtils.readValue(topicAllSetting.replaceAll("\r\n|\r|\n", ""));
				QuestionnaireSetTopic topic = new QuestionnaireSetTopic();
				topic = topicService.getSaveTopicInfo(map, setId, "check");
				listTopic.add(topic);
				List<QuestionnaireSetItems> listItem = itemService.getSaveItemsInfo(itemSetting, topic.getId());
				listItems.addAll(listItem);
			}
		}
		if (fillInBlanks != null && fillInBlanks.length > 0) {
			for (int i = 0; i < fillInBlanks.length; i++) {
				String times = fillInBlanks[i];
				String topicAllSetting = request.getParameter(times + "fillInBlanks");
				String itemSetting = request.getParameter(times + "option");
				Map<String, Object> map = JSONObject.parseObject(topicAllSetting);
				QuestionnaireSetTopic topic = new QuestionnaireSetTopic();
				topic = topicService.getSaveTopicInfo(map, setId, "fillInBlanks");
				listTopic.add(topic);
				List<QuestionnaireSetItems> listItem = itemService.getSaveItemsInfo(itemSetting, topic.getId());
				listItems.addAll(listItem);
			}
		}
		if (shortAnswer != null && shortAnswer.length > 0) {
			for (int i = 0; i < shortAnswer.length; i++) {
				String times = shortAnswer[i];
				String topicAllSetting = request.getParameter(times + "shortAnswer");
				String itemSetting = request.getParameter(times + "option");
				Map<String, Object> map = JsonUtils.readValue(topicAllSetting.replaceAll("\r\n|\r|\n", ""));
				QuestionnaireSetTopic topic = new QuestionnaireSetTopic();
				topic = topicService.getSaveTopicInfo(map, setId, "shortAnswer");
				listTopic.add(topic);
				List<QuestionnaireSetItems> listItem = itemService.getSaveItemsInfo(itemSetting, topic.getId());
				listItems.addAll(listItem);
			}
		}
		if (answer != null && answer.length > 0) {
			for (int i = 0; i < answer.length; i++) {
				String times = answer[i];
				String topicAllSetting = request.getParameter(times + "answer");
				String itemSetting = request.getParameter(times + "option");
				Map<String, Object> map = JSONObject.parseObject(topicAllSetting);
				QuestionnaireSetTopic topic = new QuestionnaireSetTopic();
				topic = topicService.getSaveTopicInfo(map, setId, "answer");
				listTopic.add(topic);
				List<QuestionnaireSetItems> listItem = itemService.getSaveItemsInfo(itemSetting, topic.getId());
				listItems.addAll(listItem);
			}
		}
		if (field != null && field.length > 0) {
			for (int i = 0; i < field.length; i++) {
				String times = field[i];
				String topicAllSetting = request.getParameter(times + "field");
				String itemSetting = request.getParameter(times + "option");
				Map<String, Object> map = JsonUtils.readValue(topicAllSetting.replaceAll("\r\n|\r|\n", ""));
				QuestionnaireSetTopic topic = new QuestionnaireSetTopic();
				topic = topicService.getSaveTopicInfo(map, setId, "field");
				listTopic.add(topic);
				List<QuestionnaireSetItems> listItem = itemService.getSaveItemsInfo(itemSetting, topic.getId());
				listItems.addAll(listItem);
			}
		}
		if (scale != null && scale.length > 0) {
			for (int i = 0; i < scale.length; i++) {
				String times = scale[i];
				String topicAllSetting = request.getParameter(times + "scale");
				String itemSetting = request.getParameter(times + "option");
				Map<String, Object> map = JsonUtils.readValue(topicAllSetting.replaceAll("\r\n|\r|\n", ""));
				QuestionnaireSetTopic topic = new QuestionnaireSetTopic();
				topic = topicService.getSaveTopicInfo(map, setId, "scale");
				listTopic.add(topic);
				List<QuestionnaireSetItems> listItem = itemService.getSaveItemsInfo(itemSetting, topic.getId());
				listItems.addAll(listItem);
			}
		}
		if (mulscale != null && mulscale.length > 0) {
			for (int i = 0; i < mulscale.length; i++) {
				String times = mulscale[i];
				String topicAllSetting = request.getParameter(times + "mulscale");
				String itemSetting = request.getParameter(times + "option");
				String itemChildSetting = request.getParameter(times + "childoption");
				Map<String, Object> map = JsonUtils.readValue(topicAllSetting.replaceAll("\r\n|\r|\n", ""));
				QuestionnaireSetTopic topic = new QuestionnaireSetTopic();
				topic = topicService.getSaveTopicInfo(map, setId, "mulscale");
				listTopic.add(topic);
				List<QuestionnaireSetItems> listItem = itemService.getSaveItemsInfo(itemSetting, itemChildSetting,
						topic.getId());
				listItems.addAll(listItem);
			}
		}
		if (illustrate != null && illustrate.length > 0) {
			for (int i = 0; i < illustrate.length; i++) {
				String times = illustrate[i];
				String topicAllSetting = request.getParameter(times + "illustrate");
				Map<String, Object> map = JsonUtils.readValue(topicAllSetting.replaceAll("\r\n|\r|\n", ""));
				QuestionnaireSetTopic topic = new QuestionnaireSetTopic();
				topic = topicService.getSaveTopicInfo(map, setId, "illustrate");
				listTopic.add(topic);
			}
		}
		if (paging != null && paging.length > 0) {
			for (int i = 0; i < paging.length; i++) {
				String times = paging[i];
				String topicAllSetting = request.getParameter(times + "paging");
				Map<String, Object> map = JsonUtils.readValue(topicAllSetting.replaceAll("\r\n|\r|\n", ""));
				QuestionnaireSetTopic topic = new QuestionnaireSetTopic();
				topic = topicService.getSaveTopicInfo(map, setId, "paging");
				topic.setTopicSetting("{\"page\":\"" + (i + 1) + "\",\"herePage\":\"" + map.get("herePage")
						+ "\",\"totalPages\":\"" + map.get("totalPages") + "\"}");
				listTopic.add(topic);
			}
		}
		QuestionnaireSet entity = questionnaireSetService.findOne(setId);
		Map<String,Object> msg=new HashMap<String,Object>(1);
		if (entity != null) {
			String sumTopicNum = ValidateUtils.filter(request.getParameter("sumTopicNum"));
			if(StringUtils.isBlank(isImport)) {
        		entity.setSumTopicNum(sumTopicNum);
    		}else if(StringUtils.isNotBlank(entity.getSumTopicNum())){
    			entity.setSumTopicNum(String.valueOf(Long.valueOf(entity.getSumTopicNum())+Long.valueOf(sumTopicNum)));
    		}
			questionnaireSetService.save(entity);
			List<String> topicList = topicService.getTopicIdBySetId(setId);
			if (topicList != null && topicList.size() > 0) {
				if(StringUtils.isBlank(isImport)) {
					itemService.deleteItemsByTopicId(topicList);
				}
			}
			if(StringUtils.isBlank(isImport)) {
				topicService.deleteTopicBySetId(setId);
			}
			List<String> queIdsList = new ArrayList<>();
			queIdsList.add(setId);
			topicService.save(listTopic);
			itemService.save(listItems);
			String clickBtnType = request.getParameter("clickBtnType");
			if ("preview".equals(clickBtnType)) {
				Map<String, Boolean> map = new HashMap<String, Boolean>();
				map.put("noMessageBox", true);
				return OperationResult.buildSuccessResult(map);
			}
			msg.put("i18NCode","oa.que.savesuccess");
			return OperationResult.buildSuccessResult("保存成功",msg);
		} else {
			msg.put("i18NCode","oa.que.haddeleted");
			return OperationResult.buildFailureResult("该题库已删除",msg);
		}
	}

}